from sanic import Blueprint, Request, json
from sanic_ext import validate
import pydantic

from ...agent import Agent

b_task = Blueprint("task", url_prefix="/task")


class SNSMTaskReq(pydantic.BaseModel):
    job_id: int


class TaskRsp(pydantic.BaseModel):
    success: bool
    msg: str
    code: int


@b_task.post("/interrupt", version=1)
@validate(json=SNSMTaskReq)
async def interrupt(request: Request, body: SNSMTaskReq):
    agent: Agent = request.app.ctx.agent
    result = await agent.task_scheduler.interrupt(body.job_id)
    return json(TaskRsp(success=result.code == 0, msg=result.msg, code=result.code).model_dump())
